/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.team.app.dao;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;

/**
 *
 * @author tunglt11
 */
public abstract class AbstractDao<T> {
	@Autowired
	private SessionFactory sessionFactory;

	private Class<T> entityClass;

	public AbstractDao(Class<T> entityClass) {
		this.entityClass = entityClass;
	}

	public Long create(T entity) {
		getCurrentSession().persist(entity);
	    Long id=(Long)getCurrentSession().getIdentifier(entity);
	    return id;
	}

	public void edit(T entity) {
		getCurrentSession().update(entity);
	}

	public void remove(T entity) {		
		getCurrentSession().delete(entity);
	}

	@SuppressWarnings("unchecked")
	public T find(Object id) {		
		return (T) getCurrentSession().get(entityClass, (Serializable) id);
	}

	@SuppressWarnings("unchecked")
	public List<T> findAll() {		
		return getCurrentSession().createQuery("from " + entityClass.getName()).list();		
	}

	public List<T> findRange(int[] range) {		
		return null;
	}

	public Long count() {
		return 0l;
	}

	public Session getCurrentSession() {
		Session  session = sessionFactory.getCurrentSession();
		if(session == null){
			session = sessionFactory.openSession();
		}		
		return session;
	}

}
